/*
 * @(#)XfdmSenhadjiDialog.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


package xfuzzy.xfdm.view;

import xfuzzy.util.*;
import xfuzzy.xfdm.model.XfdmAlgorithm;
import xfuzzy.xfdm.model.algorithm.XfdmSenhadji;
import xfuzzy.xfdm.view.listener.*;
import javax.swing.*;
import java.awt.*;

/**
 * Ventana de configuraci�n del algoritmo de Senhadji
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfdmSenhadjiDialog extends JDialog implements IXfdmAlgorithmDialog, IXfdmFrame {

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603017L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Algoritmo a configurar
	 */
	private XfdmSenhadji senhadji;
	
	/**
	 * Campo para introducir el n�mero de reglas a generar
	 */
	private XTextField number;
	
	/**
	 * Marcador de configuraci�n realizada
	 */
	private boolean conf;

	//----------------------------------------------------------------------------//
	//                               CONSTRUCTOR                                  //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfdmSenhadjiDialog(Xfdm xfdm, XfdmAlgorithm algorithm){
		super(xfdm,"Xfdm",true);
		if(algorithm != null && algorithm instanceof XfdmSenhadji) {
			XfdmSenhadji alg = (XfdmSenhadji) algorithm;
			this.senhadji = (XfdmSenhadji) alg.clone();
		} else {
			this.senhadji = new XfdmSenhadji();
		}
		build();
		set();
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Muestra el di�logo
	 */
	public static XfdmSenhadji showDialog(Xfdm xfdm, XfdmAlgorithm algorithm) {
		XfdmSenhadjiDialog dialog = new XfdmSenhadjiDialog(xfdm, algorithm);
		dialog.setVisible(true);
		return dialog.getAlgorithm();
	}

	/**
	 * Obtiene el algoritmo
	 */
	public XfdmSenhadji getAlgorithm() {
		if(this.conf) return this.senhadji;
		return null;
	}

	/**
	 * Acci�n de almacenar los datos y cerrar la ventana
	 */
	public void actionSet() {
		try { get(); } catch(Exception ex) { return; }
		this.conf = true;
		setVisible(false); 
	}

	/**
	 * Acci�n de cerrar la ventana
	 */
	public void actionCancel() {
		this.conf = false;
		setVisible(false); 
	}

	/**
	 * Acci�n de cerrar la ventana
	 */
	public void close() {
		this.conf = false;
		setVisible(false); 
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Generaci�n de la ventana
	 */
	private void build() {
		String lb[] = {"Set", "Cancel"};
		XCommandForm form = new XCommandForm(lb,lb,new XfdmAlgorithmActionListener(this));
		form.setCommandWidth(150);
		form.block();

		number = new XTextField("");

		JPanel lpanel = new JPanel();
		lpanel.setLayout(new GridLayout(1,2));
		lpanel.add(new XLabel("Number of rules"));
		lpanel.add(number);

		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Parameters selection for Senhadji algorithm"));
		content.add(Box.createVerticalStrut(5));
		content.add(lpanel);
		content.add(Box.createVerticalStrut(5));
		content.add(form);

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new XfdmWindowListener(this));
		pack();
		setLocation();
	}

	/**
	 * Coloca la ventana en la pantalla	
	 */
	private void setLocation() {
		Dimension frame = getSize();
		Dimension screen = getToolkit().getScreenSize();
		setLocation((screen.width - frame.width)/2,(screen.height - frame.height)/2);
	}

	/**
	 * Actualiza los par�metros de configuraci�n del algoritmo
	 */
	private void set() {
		number.setText(""+senhadji.getNumberOfRules());
	}

	/**
	 * Detecta posibles errores en los valores de los par�metros
	 */
	private boolean get() {
		try { senhadji.setNumberOfRules(Integer.parseInt(number.getText().trim())); }
		catch(Exception ex) {
			number.setText("");
			XDialog.showMessage(number,"Not a valid identifier");
			return false;
		}
		return true;
	}
}


